<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Patient extends CI_Controller {
    //création attribut id_patient = pour accèder à l'identifiant du patient au nniveau de la classe

	function __construct()
	    {
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
                $this->load->library('input');
                $this->load->model('mod_patient');
                $this->load->library('lang');
                $this->load->library('uri');
	    }

	function index()
	{
		$this->load->view('patient/vue_updatePatient');
	}
        /*module d'inscription de patient*/
        function addComptePatient(){
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email|is_unique[compte_patient.EMAIL]');
                $this->form_validation->set_rules('civilite','"Civilite"','required');
                $this->form_validation->set_rules('nom','"Nom"','trim|required|xss_clean');
                $this->form_validation->set_rules('prenom','"Prenom"','trim|required|xss_clean');
                $this->form_validation->set_rules('password_patient','"Mot de passe"','trim|required|xss_clean');
                $this->form_validation->set_rules('lu','"Cocher condition d\'utilisation"','required');
		if ($this->form_validation->run()==FALSE){
                    echo '<script>document.location="../";</script>';
                    
                } else { 
			$email = $this->input->post('email');
				$data = array(
					'CIVILITE_PA' => $this->input->post('civilite') ,
					'NOM_PA' => $this->input->post('nom') ,
					'PRENOM_PA' => $this->input->post('prenom')
				);
                                $compte = array(
                                    'EMAIL'=>$email ,
                                    'MDP'=> $this->input->post('password_patient'),
                                    'ETAT'=>0
                                );
				$this->mod_patient->addComptePatient($compte);
                                 $id_compte= $this->mod_patient->getIdCompteByMail($email);
                                 $data['ID_COMPTE_PA']=$id_compte;
                                if($this->mod_patient->addInfoPatient($data)){
                                    echo '<script>alert("INSCRIPTION REUSSIE");document.location="../";</script>';
                                }
                                
                    }
            
        }
        
        function connexionPatient(){           
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email');
                $this->form_validation->set_rules('password_patient','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('accueil');
                } else {
		$email = $this->input->post('email');
		$mdp = $this->input->post('password_patient');
		$id_compte_patient = $this->mod_patient->verifComptePatient($email,$mdp);
                $id_pa = $this->mod_patient->getPatientByIdCompte($id_compte_patient);
		if ($id_pa!=0){	  					
			$newdata = array(
			 'idemail'  => $email,
			 'logged_in' => 'patient',
			'idid' => $id_pa
		   );          
                    $this->session->set_userdata($newdata);
                    $_SESSION['idemail'] = $email;	
                    $_SESSION['logged_in'] = 'patient';	
                    $_SESSION['idid'] = $id_pa;
                    echo $id_pa;
			//header("location:updateP1?id=".$result);
                   //echo '<script>document.location = "updateP1?id='.$id_pa.'";</script>';
                   /*echo '<script>document.location="espace_patient?id='.$result.'";</script>';*/
                    //création attribut id_patient
		}
		else {
			//echo "ts ok";
			/*echo '<script>alert("Erreur login ou mot de passe")</script>';
			$this->load->view('pagedn/header');	
			$this->load->view('pagedn/ajoutP1');	
			$this->load->view('pagedn/footer');	*/
			echo '<script>alert("Erreur login ou mot de passe"); document.location = "../index.php";</script>';
                    
		}
            }
            
            
            
            
            
            
        }
        function espace_patient(){
                $lg = $this->uri->segment(1,0);
                $_SESSION['lg']=$lg;
                /*$this->lang->load('header',$lg);
                $this->lang->load('lien',$lg);
                $this->lang->load('container',$lg);
                $this->lang->load('footer',$lg);*/
                $tache=$this->uri->segment(3);
                if($tache){
                    switch ($tache){
                        case 'voir':echo "voir"; 

                            break;
                        case 'editer':echo "editer"; 
                    }
                }else {
                    echo "rien";
                }
            
        }
        
        
}